********************************************************************************
* Exporting figure 1 stats for multi-country COVID paper (SLE1 Sample)
* last updated: 2020-11-05
* Salim Benhachmi
********************************************************************************

clear all
use "$da/SLE1", clear

********************************************************************************

gen week_date = 1
replace week_date = 2 if survey_date >= `=date("04may2020", "DMY")'
replace week_date = 3 if survey_date >= `=date("11may2020", "DMY")'
replace week_date = 4 if survey_date >= `=date("18may2020", "DMY")'
replace week_date = 5 if survey_date >= `=date("25may2020", "DMY")'
replace week_date = 6 if survey_date >= `=date("01jun2020", "DMY")'
replace week_date = 7 if survey_date >= `=date("08jun2020", "DMY")'
replace week_date = 8 if survey_date >= `=date("15jun2020", "DMY")'
replace week_date = 9 if survey_date >= `=date("22jun2020", "DMY")'
replace week_date = 10 if survey_date >= `=date("29jun2020", "DMY")'
replace week_date = 11 if survey_date >= `=date("06jul2020", "DMY")'

********************************************************************************

* reduced portions
gen port_ad = .
replace port_ad = 0 if reduce_male == 0
replace port_ad = 0 if reduce_female == 0
replace port_ad = 1 if reduce_male == 1
replace port_ad = 1 if reduce_female == 1
gen port_ch = .
replace port_ch = 0 if reduce_boy == 0
replace port_ch = 0 if reduce_girl == 0
replace port_ch = 1 if reduce_boy == 1
replace port_ch = 1 if reduce_girl == 1

* food spending
gen pre_sp_rice = pre_pr_rice * pre_qt_rice
gen pre_sp_cass = pre_pr_cass * pre_qt_cass
gen pre_sp_fish = pre_pr_fish * pre_qt_fish
gen pre_sp_oil = pre_pr_oil * pre_qt_oil
gen pre_sp_maggi = pre_pr_maggi * pre_qt_maggi

gen sp_rice = pr_rice * qt_rice
gen sp_cass = pr_cass * qt_cass
gen sp_fish = pr_fish * qt_fish
gen sp_oil = pr_oil * qt_oil
gen sp_maggi = pr_maggi * qt_maggi

gen pre_sp_food = pre_sp_rice + pre_sp_cass + pre_sp_fish + pre_sp_oil + pre_sp_maggi
gen sp_food = sp_rice + sp_cass + sp_fish + sp_oil + sp_maggi

* BL spending shares
gen sh_rice = pre_sp_rice / pre_sp_food
gen sh_cass = pre_sp_cass / pre_sp_food
gen sh_fish = pre_sp_fish / pre_sp_food
gen sh_oil = pre_sp_oil / pre_sp_food
gen sh_maggi = pre_sp_maggi / pre_sp_food

mean sh_rice-sh_maggi
matrix b = r(table)
matrix list b

local sh_rice = b[1,1]
local sh_cass = b[1,2]
local sh_fish = b[1,3]
local sh_oil = b[1,4]
local sh_maggi = b[1,5]

********************************************************************************

local indicators port_ad port_ch pr_rice pr_cass pr_fish pr_oil pr_maggi ///
	sp_food sp_rice sp_cass sp_fish sp_oil sp_maggi

foreach ind of local indicators {
	
	mean pre_`ind'
	matrix b = r(table)
	matrix list b
	local 0_`ind' = round(b[1,1], 0.01)
	local 0_`ind'_n = b[7,1]
	
	foreach n of numlist 1/11 {
	
		mean `ind' if week_date == `n'
		matrix b = r(table)
		matrix list b
		local `n'_`ind' = round(b[1,1], 0.01)
		local `n'_`ind'_n = b[7,1]
	
	}

}

local indicators2 tot_profits

foreach ind of local indicators2 {
	
	mean bl_`ind' if inlist(survey_round, "Rural Round 1")
	matrix b = r(table)
	matrix list b
	local 0_`ind' = round(b[1,1], 0.01)
	local 0_`ind'_n = b[7,1]
	
	foreach n of numlist 3/11 {
	
		mean `ind' if week_date == `n'
		matrix b = r(table)
		matrix list b
		local `n'_`ind' = round(b[1,1], 0.01)
		local `n'_`ind'_n = b[7,1]
	
	}

}

local food_items pr_rice pr_cass pr_fish pr_oil pr_maggi

foreach item of local food_items {

	replace `item' = `item' / `0_`item'' - 1
	replace pre_`item' = pre_`item' / `0_`item'' - 1

}

gen pr_food = `sh_rice' * pr_rice + `sh_cass' * pr_cass + `sh_fish' * ///
	pr_fish + `sh_oil' * pr_oil + `sh_maggi' * pr_maggi
	
gen pre_pr_food = `sh_rice' * pre_pr_rice + `sh_cass' * pre_pr_cass + ///
	`sh_fish' * pre_pr_fish + `sh_oil' * pre_pr_oil + `sh_maggi' * pre_pr_maggi

preserve
keep sp_food pr_food tot_profits port_ad port_ch
gen period = 1
tempfile post
save `post'
restore

keep pre_sp_food pre_pr_food bl_tot_profits pre_port_ad pre_port_ch survey_round
rename (pre_sp_food pre_pr_food bl_tot_profits pre_port_ad pre_port_ch) ///
	(sp_food pr_food tot_profits port_ad port_ch)
replace tot_profits = . if survey_round != "Rural Round 1"
gen period = 0
append using `post'

local pvalues sp_food tot_profits port_ad port_ch

foreach p of local pvalues {

	reg `p' period
	matrix b = r(table)
	matrix list b
	local avg_`p' = b[1,1] / b[1,2]
	local p_`p' = b[4,1]
	
}

reg pr_food period
matrix b = r(table)
matrix list b
local avg_pr_food = b[1,1]
local p_pr_food = b[4,1]

clear
set obs 12
gen week = _n
replace week = week - 1

foreach ind of local indicators {

	gen `ind' = .
	gen `ind'_n = .

	foreach n of numlist 0/11 {
		replace `ind' = ``n'_`ind'' if week == `n'
		replace `ind'_n = ``n'_`ind'_n' if week == `n'
	
	}
	
}

foreach ind of local indicators2 {

	gen `ind' = .
	gen `ind'_n = .
	
	replace `ind' = `0_`ind'' if week == 0
	replace `ind'_n = `0_`ind'_n' if week == 0

	foreach n of numlist 3/11 {
		replace `ind' = ``n'_`ind'' if week == `n'
		replace `ind'_n = ``n'_`ind'_n' if week == `n'
	
	}
	
}

local indicators3 port_ad port_ch tot_profits pr_rice pr_cass pr_fish pr_oil ///
	pr_maggi sp_food sp_rice sp_cass sp_fish sp_oil sp_maggi

foreach ind of local indicators3 {

	replace `ind' = `ind' / `0_`ind'' - 1 

}

gen pr_food = `sh_rice' * pr_rice + `sh_cass' * pr_cass + `sh_fish' * ///
	pr_fish + `sh_oil' * pr_oil + `sh_maggi' * pr_maggi

gen pr_food_n = max(pr_rice_n, pr_cass_n, pr_fish_n, pr_oil_n, pr_maggi_n)

********************************************************************************

rename (port_ad port_ch tot_profits sp_food pr_food port_ad_n port_ch_n ///
	tot_profits_n sp_food_n pr_food_n) (adult_skipped_meals ///
	child_skipped_meals enterprise_profits food_consumption food_prices ///
	adult_skipped_meals_n child_skipped_meals_n enterprise_profits_n ///
	food_consumption_n food_prices_n)
keep week adult_skipped_meals child_skipped_meals food_consumption ///
	enterprise_profits food_prices adult_skipped_meals_n ///
	child_skipped_meals_n food_consumption_n enterprise_profits_n food_prices_n
	
order week food_consumption food_consumption_n food_prices food_prices_n ///
	enterprise_profits enterprise_profits_n adult_skipped_meals ///
	adult_skipped_meals_n child_skipped_meals child_skipped_meals_n 

gen week_beginning = ""
order week week_beginning
replace week_beginning = "March 2020" in 1
replace week_beginning = "Apr 27" in 2
replace week_beginning = "May 4" in 3
replace week_beginning = "May 11" in 4
replace week_beginning = "May 18" in 5
replace week_beginning = "May 25" in 6
replace week_beginning = "June 1" in 7
replace week_beginning = "June 8" in 8
replace week_beginning = "June 15" in 9
replace week_beginning = "June 22" in 10
replace week_beginning = "June 29" in 11
replace week_beginning = "July 6" in 12

preserve

clear
set obs 2
gen num = _n
gen indicator = ""
replace indicator = "avg_change" in 1
replace indicator = "p_value" in 2
gen food_consumption = .
gen food_prices = .
gen enterprise_profits = .
gen adult_skipped_meals = .
gen child_skipped_meals = .
replace food_consumption = `avg_sp_food' if num == 1
replace food_consumption = `p_sp_food' if num == 2
replace food_prices = `avg_pr_food' if num == 1
replace food_prices = `p_pr_food' if num == 2
replace enterprise_profits = `avg_tot_profits' if num == 1
replace enterprise_profits = `p_tot_profits' if num == 2
replace adult_skipped_meals = `avg_port_ad' if num == 1
replace adult_skipped_meals = `p_port_ad' if num == 2
replace child_skipped_meals = `avg_port_ch' if num == 1
replace child_skipped_meals = `p_port_ch' if num == 2
drop num

keep if indicator == "p_value"
rename (food_consumption food_prices enterprise_profits adult_skipped_meals ///
	child_skipped_meals) (food_consumption_p food_prices_p ///
	enterprise_profits_p adult_skipped_meals_p child_skipped_meals_p)
gen week = 0

tempfile p_values
save `p_values'

restore

merge 1:1 week using `p_values', nogen

replace week = week + 1

local vars food_consumption food_prices enterprise_profits ///
	adult_skipped_meals child_skipped_meals

foreach var of local vars {
	replace `var' = `var' * 100
}

save "$da/forgraphs_SLE1", replace

keep week enterprise_profits enterprise_profits_n enterprise_profits_p
gen enterprise_profits2 = .
replace enterprise_profits2 = enterprise_profits[1] in 1
replace enterprise_profits2 = enterprise_profits[4] / 3 in 2
replace enterprise_profits2 = enterprise_profits[4] * 2 / 3 in 3
replace enterprise_profits2 = enterprise_profits[4] in 4

save "$da/forgraphs_ent_SLE1", replace

********************************************************************************
